package com.njtoyo.taxi.admin.logic.data;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.njtoyo.taxi.admin.mapper.secondary.entity.cache.CacheDriverIncomeOnlineSum;
import com.njtoyo.taxi.admin.mapper.secondary.entity.data.DashboardIncomeDaily;
import com.njtoyo.taxi.admin.mapper.secondary.entity.data.DashboardIncomeMinute;
import com.njtoyo.taxi.admin.mapper.secondary.enums.DataIncomeThroughType;
import com.njtoyo.taxi.admin.mapper.secondary.mapper.cache.CacheDriverIncomeOnlineSumMapper;
import com.njtoyo.taxi.admin.mapper.secondary.mapper.data.DashboardIncomeDailyMapper;
import com.njtoyo.taxi.admin.mapper.secondary.mapper.data.DashboardIncomeMinuteMapper;
import com.njtoyo.taxi.admin.rest.presenter.data.data_analysis.RankListRankTotalPresenter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DashboardIncomeLogic {

    @Autowired
    private DashboardIncomeDailyMapper dashboardIncomeDailyMapper;

    @Autowired
    private DashboardIncomeMinuteMapper dashboardIncomeMinuteMapper;

    @Autowired
    private CacheDriverIncomeOnlineSumMapper cacheDriverIncomeOnlineSumMapper;

    // 根据区域id、时间范围从dashboard_income_daily中获取
    public List<DashboardIncomeDaily> getOnlineAmountFromDailyGroupByCanton(List<Long> isImitates, Date startTime, Date endTime) {
        String start = DateUtil.formatDate(startTime);
        String end = DateUtil.formatDate(endTime);

        QueryWrapper<DashboardIncomeDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("IFNULL(SUM(value), 0) as total, canton_id");
        queryWrapper.in("is_imitate", isImitates);
        queryWrapper.eq("through_type", DataIncomeThroughType.all);
        queryWrapper.between("stats_date", start, end);
        queryWrapper.isNotNull("canton_id");
        queryWrapper.groupBy("canton_id");

        return dashboardIncomeDailyMapper.selectList(queryWrapper);
    }

    // 根据区域id、时间范围从dashboard_income_minute中获取
    public List<DashboardIncomeMinute> getOnlineAmountFromMinuteGroupByCanton(List<Long> isImitates, Date startTime, Date endTime) {
        String start = DateUtil.formatDateTime(startTime);

        String end = DateUtil.formatDateTime(endTime);
        QueryWrapper<DashboardIncomeMinute> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("IFNULL(SUM(value), 0) as total, canton_id");
        queryWrapper.in("is_imitate", isImitates);
        queryWrapper.eq("through_type", DataIncomeThroughType.all);
        queryWrapper.between("stats_date", start, end);
        queryWrapper.isNotNull("canton_id");
        queryWrapper.groupBy("canton_id");

        return dashboardIncomeMinuteMapper.selectList(queryWrapper);
    }

    // 根据【公司ids】、时间范围从dashboard_income_daily中获取
    public List<DashboardIncomeDaily> getOnlineAmountFromDailyGroupByCompany(List<Long> isImitates, Date startTime, Date endTime, List<Long> companyIds) {
        String start = DateUtil.formatDate(startTime);
        String end = DateUtil.formatDate(endTime);

        QueryWrapper<DashboardIncomeDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("IFNULL(SUM(value), 0) as total, company_id");
        queryWrapper.in("is_imitate", isImitates);
        queryWrapper.eq("through_type", DataIncomeThroughType.all);
        queryWrapper.between("stats_date", start, end);
        queryWrapper.in("company_id", companyIds);
        queryWrapper.groupBy("company_id");

        return dashboardIncomeDailyMapper.selectList(queryWrapper);
    }

    // 根据【公司ids】、时间范围从dashboard_income_minute中获取
    public List<DashboardIncomeMinute> getOnlineAmountFromMinuteGroupByCompany(List<Long> isImitates, Date startTime, Date endTime, List<Long> companyIds) {
        String start = DateUtil.formatDateTime(startTime);
        String end = DateUtil.formatDateTime(endTime);

        QueryWrapper<DashboardIncomeMinute> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("IFNULL(SUM(value), 0) as total, company_id");
        queryWrapper.in("is_imitate", isImitates);
        queryWrapper.eq("through_type", DataIncomeThroughType.all);
        queryWrapper.between("stats_date", start, end);
        queryWrapper.in("company_id", companyIds);
        queryWrapper.groupBy("company_id");

        return dashboardIncomeMinuteMapper.selectList(queryWrapper);
    }

    // 根据【司机ids】、时间范围从cache_driver_income_online_sum中获取
    public List<RankListRankTotalPresenter> getOnlineAmountGroupByDriver(Date startTime, Date endTime, List<Long> driverIds) {
        String startTimeStr = DateUtil.formatDate(startTime);
        String endTimeStr = DateUtil.formatDate(endTime);

        List<RankListRankTotalPresenter> totalPresenter = new ArrayList<>();

        QueryWrapper<CacheDriverIncomeOnlineSum> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("IFNULL(SUM(`value`), 0) as total, driver_id");
        queryWrapper.between("DATE_FORMAT(CONCAT_WS('-', `year`, `month`, `day`), '%Y-%m-%d')", startTimeStr, endTimeStr);
        queryWrapper.in("driver_id", driverIds);
        queryWrapper.groupBy("driver_id");

        List<CacheDriverIncomeOnlineSum> driverIncomeOnlineSums = cacheDriverIncomeOnlineSumMapper.selectList(queryWrapper);

        driverIncomeOnlineSums.forEach(i -> {
            RankListRankTotalPresenter rank = new RankListRankTotalPresenter();
            rank.setDriverId(i.getDriverId());
            rank.setTotalValue(i.getTotal());

            totalPresenter.add(rank);
        });

        return totalPresenter;
    }
}
